Table of Contents
Previous Section
WebScript uses a subset of Objective-C syntax, but its role within an application is significantly different. The following table summarizes some of the differences.
Objective-C WebScript _____________________________________________________________________________ Is compiled Is interpreted Supports primitive C data types Only supports the id data type Requires method prototyping Doesn't require method prototyping (that is, you don't declare methods before you use them) Usually includes a .h and a .m file Usually has corresponding declarations and HTML template files (unless it is an application script) Supports all C language features Has limited support for C language features; for example, doesn't support structures, pointers, enumerators, or unions Methods not declared to return void Methods aren't required to include a must include a return statement return statement Has pre-processor support Has no pre-processor support---that is, doesn't support the #import or #include statements _____________________________________________________________________________
Perhaps the most significant difference between Objective-C and WebScript is that in WebScript, the only valid data type is id. Some of the less obvious implications of this are:
// NO!! This won't work. string = [NSString stringWithCString:"my string"];
// This is fine. [self logWithFormat:@"The value is %@", myVar]; // NO!! This won't work. [self logWithFormat:@"The values are %d and %s", var1, var2];
// This is fine. - aMethod:anArg { // NO!! This won't work. - (void) aMethod:(NSString *)anArg { // This won't work either - (id)aMethod:(id)anArg {
For example, suppose you want to compare two numeric values using the enumerated type NSComparisonResult. This is how you might do it in Objective-C:
result = [num1 compare:num2]; if(result == NSOrderedAscending) /* This won't work in WebScript */ /* num1 is less than num2 */
But this won't work in WebScript. Instead, you have to use the integer value of NSOrderedAscending, as follows:
result = [num1 compare:num2]; if(result == -1) /* num1 is less than num2 */